﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CUiEMEndModelingEntityLevelCmd
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using COMMANDPARAMSLib;
using DnProcessSimulateCommands.EndModelingEntityLevel;
using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Linq;
using Tecnomatix.Engineering;


namespace DnProcessSimulateCommands;

public class CUiEMEndModelingEntityLevelCmd : ITxCommand, ITxExecutableCommand
{
  private TxCommandEnabler m_enabler = new TxCommandEnabler(true);

  public ITxCommandEnabler CommandEnabler => (ITxCommandEnabler) this.m_enabler;

  public bool Connect() => true;

  public void Disconnect()
  {
  }

  public void Execute(object cmdParams)
  {
    if (!(cmdParams is CoEMEndModelingEntityLevelParams entityLevelParams))
      return;
    CApEMEndModelingEntityLevelApp endModelingApp = this.CreateEndModelingApp();
    endModelingApp.ConsiderNonModelingStateWithoutUI = Convert.ToBoolean(((ICoEMEndModelingEntityLevelParams) entityLevelParams).ConsiderStateWithoutUI);
    ((ICoEMEndModelingEntityLevelParams) entityLevelParams).EndModeling = Convert.ToInt32(endModelingApp.ComponentEndModeling());
    ((ICoEMEndModelingEntityLevelParams) entityLevelParams).ReloadInDetailRep = Convert.ToInt32(endModelingApp.ReloadInDetailRepresentation);
    ((ICoEMEndModelingEntityLevelParams) entityLevelParams).ReloadWithPMI = Convert.ToInt32(endModelingApp.ReloadPMIs);
    ((ICoEMEndModelingEntityLevelParams) entityLevelParams).ReloadWithCommand = Convert.ToInt32(endModelingApp.ReloadWithCommand);
  }

  private CApEMEndModelingEntityLevelApp CreateEndModelingApp()
  {
    CApEMEndModelingEntityLevelApp endModelingApp = (CApEMEndModelingEntityLevelApp) null;
    TxObjectList allItems = TxApplication.ActiveSelection.GetAllItems();
    if (((Collection<ITxObject>) allItems).Count == 1)
      endModelingApp = new CApEMEndModelingEntityLevelApp(((Collection<ITxObject>) allItems)[0] as ITxComponent);
    else if (((Collection<ITxObject>) allItems).Count > 1)
      endModelingApp = (CApEMEndModelingEntityLevelApp) new CApEMEndModelingMultipleComponentsEntityLevelApp(this.FilterComponentsFromSelection());
    return endModelingApp;
  }

  private TxObjectList<ITxComponent> FilterComponentsFromSelection()
  {
    TxTypeFilter txTypeFilter = new TxTypeFilter(typeof (ITxComponent));
    txTypeFilter.AddExcludedType(typeof (ITxTCPTrackObject));
    return new TxObjectList<ITxComponent>(((IEnumerable) TxApplication.ActiveSelection.GetFilteredItems((ITxTypeFilter) txTypeFilter)).Cast<ITxComponent>());
  }

  public bool Expose => false;

  public string Identifier => string.Empty;
}
